package com.example.xuegengwang.xuegengwang.fragment;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.xuegengwang.xuegengwang.R;

import java.util.ArrayList;

import common.BaseFragment;
import common.BaseFragmentAdapter;
import https.handler.ErrorPageHandler;
import secondFragment.fragment.OnlineAskFragment;
import secondFragment.fragment.PopularQuestionFragment;

import view.MyPagerSlidingTabStrip;

/**
 * 提问fragment
 */
@SuppressLint("ValidFragment")
public class SecondFragment extends BaseFragment {

    private View view;
    private ViewPager second_fragment_view_pager;
    private FragmentManager fragmentManager;
    private PopularQuestionFragment popularQuestionFragment;//热门提问
    private OnlineAskFragment onlineAskFragment;//在线提问
    private ArrayList<Fragment> arrayList;
    private MyPagerSlidingTabStrip tabStrip;//滑动标签

    private String tabs1, tabs2;
    private int tabs_color, tabs_text_size, tabs_color_chosed;
    private static final String TAG = "SecondFragment";

    private String[] tabs;

    private ErrorPageHandler myHandler = new ErrorPageHandler(true) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_second, container, false);
        if (getMyActivity() != null) {
            initData();
            initView();
            initFragment();
        }
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (onlineAskFragment != null && second_fragment_view_pager.getCurrentItem()==1) {
                //当在线提问可见时
                onlineAskFragment.reSetData();
            }
        }
    }

    private void initData() {
        tabs1 = getMyActivity().getResources().getString(R.string.second_fragment_tabs_item1);
        tabs2 = getMyActivity().getResources().getString(R.string.second_fragment_tabs_item2);
        tabs_color_chosed = getMyActivity().getResources().getColor(R.color.white);
        tabs_color = getMyActivity().getResources().getColor(R.color.tab_color);
        tabs_text_size = getMyActivity().getResources().getDimensionPixelSize(R.dimen.text_size);
    }

    private void initView() {
        second_fragment_view_pager = (ViewPager) view.findViewById(R.id.second_fragment_view_pager);
        tabStrip = (MyPagerSlidingTabStrip) view.findViewById(R.id.fragment_second_tabs);
    }

    private void initFragment() {
        fragmentManager = getChildFragmentManager();
        popularQuestionFragment = new PopularQuestionFragment();
        onlineAskFragment = new OnlineAskFragment();
        arrayList = new ArrayList<>();
        arrayList.add(popularQuestionFragment);
        arrayList.add(onlineAskFragment);
        tabs = new String[]{tabs1, tabs2};
        second_fragment_view_pager.setAdapter(new BaseFragmentAdapter(fragmentManager, arrayList, tabs));
        tabStrip.setViewPager(second_fragment_view_pager, myHandler);
        initTabStrip();
    }

    private void initTabStrip() {
        tabStrip.setTextColor(tabs_color);
        tabStrip.setTextSize(tabs_text_size);
        tabStrip.setDividerColor(Color.TRANSPARENT);
        tabStrip.setDividerPadding(1);
        tabStrip.setUnderlineHeight(0);
        tabStrip.setIndicatorColor(tabs_color_chosed);
        tabStrip.setIndicatorHeight(4);
        tabStrip.setShouldExpand(true);
        tabStrip.setTabPaddingLeftRight(32);
        tabStrip.setIndicatorPadding(30, 30);
        tabStrip.setSelectedTextColor(tabs_color_chosed);
    }

    public void changePage(int position) {
        if (getMyActivity() != null) {
            if (second_fragment_view_pager == null) {
                if (getMyActivity() != null) {
                    initData();
                    initView();
                    initFragment();
                }
            }
            second_fragment_view_pager.setCurrentItem(position);
        }
    }
}